package com.doge.calcalbum.business.note

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.core.view.setPadding
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.doge.calcalbum.R
import com.doge.calcalbum.analysis.analysisEvent
import com.doge.calcalbum.analysis.page_note_list
import com.doge.calcalbum.business.ITEM_CHECK
import com.doge.calcalbum.business.account.AccountHelper.self
import com.doge.calcalbum.business.vip.VipHelper
import com.doge.calcalbum.core.BaseActivity
import com.doge.calcalbum.core.base.adapters.ITEM_CLICKED
import com.doge.calcalbum.core.base.adapters.ITEM_LONG_CLICKED
import com.doge.calcalbum.databinding.ActivityNoteBinding
import com.doge.calcalbum.db.model.Note
import com.doge.calcalbum.utils.setOnClickCallback
import com.doge.calcalbum.utils.systemWindowInsetsTop
import com.doge.calcalbum.utils.toast

class NoteActivity : BaseActivity() {
    private val binding by lazy { ActivityNoteBinding.inflate(layoutInflater) }
    private val viewModel by viewModels<NoteViewModel> { NoteViewModelFactory() }
    private val adapter by lazy { NoteAdapter() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        analysisEvent(page_note_list)
        systemWindowInsetsTop.observe(this) {
            binding.fitsSys.setPadding(it / 2)
        }

        initialViews()
        binding.recyclerView.itemAnimator = null
        binding.recyclerView.layoutManager = LinearLayoutManager(binding.recyclerView.context)
        binding.recyclerView.adapter = adapter
        adapter.setListener(::handleAdapterCallback)
        viewModel.noteList.observe(this) {
            if (it.isNullOrEmpty()) animToNormalMode()
            binding.empty.isVisible = it.isNullOrEmpty()
            adapter.submitList(it)
        }
    }

    private fun handleAdapterCallback(action: String, item: Any?) {
        when (action) {
            ITEM_LONG_CLICKED -> animToEditMode(item as? Note?)
            ITEM_CHECK -> adapter.switch(item as? Note?)
            ITEM_CLICKED -> {
                if (!binding.edit.isVisible) adapter.switch(item as? Note?)
                else NoteEditActivity.start(this, (item as? Note?)?.id)
            }
        }
    }

    private fun initialViews() {
        binding.pageBack.setOnClickCallback { onBackPressed() }
        binding.add.setOnClickCallback {
            if (self.isVip) {
                NoteEditActivity.start(it.context)
            } else {
                VipHelper.showBuyDialog("noteAdd", this)
            }
        }
        binding.edit.setOnClickCallback {
            animToEditMode()
        }
        binding.editCancel.setOnClickCallback {
            animToNormalMode()
        }
        binding.selectAll.setOnClickCallback {
            adapter.switchAll()
        }
        binding.delete.setOnClickCallback {
            val selected = adapter.selected
            if (selected.isEmpty()) {
                toast(R.string.please_select_entry)
            } else {
                viewModel.delete(selected)
            }
        }
    }

    private fun animToEditMode(note: Note? = null) {
        binding.editCancel.isVisible = true
        binding.selectAll.isVisible = true
        binding.bottomMenu.isVisible = true
        binding.add.isVisible = false
        binding.edit.isVisible = false
        binding.pageBack.isVisible = false
        adapter.enterEdit(note)
    }

    private fun animToNormalMode() {
        binding.editCancel.isVisible = false
        binding.selectAll.isVisible = false
        binding.bottomMenu.isVisible = false
        binding.add.isVisible = true
        binding.edit.isVisible = true
        binding.pageBack.isVisible = true
        adapter.exitEdit()
    }

    companion object {
        fun start(context: FragmentActivity?) {
            if (context == null) return
//            if (self.canGetFreeVip == 1) {
//                VipHelper.showGetFreeVipDialog()
//            } else
            if (self.isVip || self.wasVip) {
                context.startActivity(Intent(context, NoteActivity::class.java))
            } else {
                VipHelper.showBuyDialog("note", context)
            }
        }
    }
}
